/*
	This script file is part of the Sonic 3 A.I.R. script software distribution.
	Copyright (C) 2017-2024 by Eukaryot

	Published under the GNU GPLv3 open source software license, see license.txt
	or https://www.gnu.org/licenses/gpl-3.0.en.html
*/



//-------------------------------------------------------//
// Button opening a nearby door                          //
//-------------------------------------------------------//

//# address-hook(0x042cd6) end(0x042d10)
function void fn042cd6()
{
	objA0.render_flags |= render_flag.WORLD
	objA0.box_size.x = 0x10
	objA0.box_size.y = 0x10
	objA0.sprite_priority = 0x0280

	if (global.act == 0)
	{
		objA0.mapping_offset = 0x042d7c
		objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE3 | 0x03a1)
	}
	else
	{
		objA0.mapping_offset = 0x042d9e
		objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE1 | 0x040d)
	}

	objA0.update_address = 0x042d16
	fn042d16()
}


//# address-hook(0x042d16) end(0x042d76)
function void fn042d16()
{
	D1.u16 = 0x10
	D2.u16 = 0x0f
	D3.u16 = 0x10
	D4.u16 = objA0.position.x.u16
	fn01dc56()

	objA0.animation.sprite = 0

	D0.u16 = objA0.subtype2c & 0x0f
	A3 = 0xfffff7e0 + D0.u16	// This points to one of "level.trigger00" ... "level.trigger0f"

	D3 = (objA0.subtype2c & 0x40) ? 7 : 0
	D6 = (D6 << 16) + (D6 >> 16)
	D6.u16 &= 0x03
	if (D6.u16 == 0)
	{
		// Button is released
		if ((objA0.subtype2c & 0x10) == 0)
		{
			u8[A3] &= ~(1 << D3.u8)
		}
	}
	else
	{
		// Button is pressed
		if (u8[A3] == 0)
		{
			playSound(SFX_CLICK)
		}
		u8[A3] |= (1 << D3.u8)
		objA0.animation.sprite = 1
	}

	DrawOrUnloadObject()
}
